<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Requests\ArticlePostRequest;
use App\Http\Controllers\Controller;
use App\Models\Articles;
use DB;

class ArticlesController extends Controller
{

    public function getIndex(Request $request)
    {
        $num = $request->num ?? 10;
        $keywords = $request->keywords? '&keywords='.$request->keywords : '';

        if($request->keywords){
            //加载用户列表模板
            $articles = DB::table('articles')
                ->join('users', 'articles.user_id', '=', 'users.id')
                ->join('cates', 'articles.cate_id', '=', 'cates.id')
                ->select('articles.*', 'users.username as username', 'cates.name as name')
                ->where('title', 'like', '%'.$request->keywords.'%')
                ->where('is_del', 0)
                ->orWhere('descr', 'like', '%'.$request->keywords.'%')
                ->orderBy('articles.id')
                ->paginate($num);
        }else{
            $articles = DB::table('articles')
                ->join('users', 'articles.user_id', '=', 'users.id')
                ->join('cates', 'articles.cate_id', '=', 'cates.id')
                ->select('articles.*', 'users.username as username', 'cates.name as name')
                ->where('is_del', 0)
                ->orderBy('articles.id')
                ->paginate($num);
        }

        return view('Admin/Article/index', ['articles' => $articles, 'num'=>$num, 'request'=>$request,'keywords'=>$keywords]);
    }

    public function getAdd()
    {
        $cates = CatesController::getCates();
        return view('Admin/Article/add',['cates'=>$cates]);
    }

    public function postInsert(ArticlePostRequest $request)
    {
        $data = $request->except('_token');

        // 执行文件上传
        if($request->hasFile('picture')){
            // 判断是否为有效上传文件
            if($request->file('picture')->isValid()){

                // 获取图片后缀名
                $ext = $request->file('picture')->getClientOriginalExtension();

                // 拼装随机文件名
                $newname = md5(date('YmdHis')).".".$ext;

                // 执行上传
                $res = $request->file('picture')->move(\Config::get('app.uploads'), $newname);

                if($res){
                    $data['picture'] = $newname;
                    $data['user_id'] = $request->session()->get('uid');
                    $info = Articles::insert($data);
                    if($info){
                        return redirect('admin/articles/index')->with('success', '发布成功');
                    }else{
                        return back()->with('error', '发布失败');
                    }
                }else{
                    return back()->with('error', '图片上传失败');
                }
            }else{
                return back()->with('error', '不是有效的上传文件');
            }
        }else{
            return back()->with('error', '没有上传的文件');
        }
    }

    public function getDelete($id)
    {
        $res = Articles::destroy($id);
        if($res){
            return redirect('admin/articles/index')->with('success','删除成功');
        }else{
            return back()->with('error','删除失败');
        }
    }

    public function getEdit($id){
        $cates=CatesController::getCates();
        $data=Articles::where('id',$id)->first()->toArray();
        // $res=DB::select('select * from articles a,cates c where a.cate_id=c.id &&a.cate_id=',$id);
        $res=DB::table('articles')->join('users','articles.user_id','=','users.id')->join('cates','articles.cate_id','=','cates.id')->select('articles.*','users.username','cates.name')->where('articles.id','=',$id)->get();
        // dd($res);
        return view('Admin/Article/edit',['data'=>$data,'res'=>$res,'cates'=>$cates]);
    }

    public function postUpdate(Request $request){
        $id=$request->id;
        $info=$request->except(['_token','id']);
        if($request->hasFile('picture')){
            //删除之前的图片
            $oldpic=DB::select('select * from articles where id=?',[$id])[0]['picture'];
            // dd($oldpic);
            //执行删除
            @unlink('./uploads/',$oldpic);

            //接收到上传文件的后缀名
            $ext=$request->file('picture')->getClientOriginalExtension();

            //随机一个文件名
            $picname=date('YmdHis').'.'.$ext;

            //接收文件信息
            $result=$request->file('picture')->move('./uploads/',$picname);
            $info['picture']=$picname;

            //      //将信息添加到数据库
            $res=Articles::where('id',$id)->update($info);
        }
        else{
            $res=Articles::where('id',$id)->update($info);
        }
        if($res){
            return redirect('admin/articles/index')->with('success','修改成功');
        }else{
            return back()->with('error','修改失败');
        }
    }
}
